<?php 
	//负责每日自动结算的自动程序
	//中文编码
	header("Content-type:text/html;charset=utf-8");
	include_once("base.php") ;
	class auto extends  base{
		//准备数据   配置项（比例，增加字段） 数据库  
		function __construct(){
			set_time_limit(0);
			parent::__construct();
		}
		//开始自动程序
		public function run(){
		$sql="select * from yang_currency where is_line = 1";
		$currency_list=$this->select($sql);
			foreach ($currency_list as $k =>$v){
				//获取货物最新价格
				$now_price=$this->result($v['currency_url'],$v['lever']);
				//查询该货物所有未平仓的寄售订单
				$sql1="select * from yang_sale_orders where status =0 and currency_id =".$v['currency_id'];
				$sale_orders=$this->select($sql1);
				foreach($sale_orders as $kk =>$vv){
					//获取该订单的利率
					$ratesql="select rate from yang_sale_config where id =".$vv['sale_type'];
					$rate=$this->find($ratesql)['rate'];
					//买涨
					if($vv['type']==1){
						//计算盈亏金钱
						$profit=($now_price-$vv['buy_point'])/$rate;
						//计算实际金钱
						$money_now=$vv['money']+$profit;
						//如果实际金钱超出涨跌停设置，平仓
						if($money_now<=$vv['fall_money']||$money_now>=$vv['rise_money']){
							//给用户加实际金钱
							$profitsql="update yang_member set rmb = rmb +".$money_now." where member_id = ".$vv['member_id'];
							$this->query($profitsql);
							//修改寄售订单状态
							$statussql="update yang_sale_orders set status = 1 , trade_time = ".time()." profit = ".$money_now." where id= ".$vv['id'];
							$this->query($statussql);
						}
					}
					//买跌
					if($vv['type']==2){
						//计算盈亏金钱
						$profit=($vv['buy_point']-$now_price)/$rate;
						//计算实际金钱
						$money_now=$vv['money']+$profit;
						//如果实际金钱超出涨跌停设置，平仓
						if($money_now<=$vv['fall_money']||$money_now>=$vv['rise_money']){
							//给用户加实际金钱
							$profitsql="update yang_member set rmb = rmb +".$money_now." where member_id = ".$vv['member_id'];
							$this->query($profitsql);
							//修改寄售订单状态
							$statussql="update yang_sale_orders set status = 1 , trade_time = ".time()." profit = ".$money_now." where id= ".$vv['id'];
							$this->query($statussql);
						}
					}
				}
				
			}
		}
		private function result($code,$lever){
		
		//$url = "http://data.ronmei.com:8888/Price?code=CL1712";
		$url = "http://data.ronmei.com:8888/Price?code=".$code;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		$result = curl_exec($ch);
		curl_close ($ch);
		$newresult=json_decode($result);
		return $newresult->Price*$lever;
	}
	
	}
	$auto=new auto();
	$auto->run();
	
?>